package net.hhk.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import net.hhk.connect.HTTPConnect;
import net.hhk.data.Constant;
import net.hhk.data.Tweet;

import android.content.Context;
import android.graphics.Color;

public class TweetManager {
	private List<Tweet> tweets;
	private static String[] name = { "Rohit", "Amit", "John", "Suresh",
			"Saurabh", "Praveen", "Hemant", "Abhay", "Amol", "Harman", "Atul",
			"Nitin", "Sumant", "Ketan", "Rahul", "Priya", "Tejaswini", "Babu",
			"Vaibhav", "Harish" };
	private static String[] messages = { "Hi my name is Rohit", "Hi I am Amit",
			"What a fine day?", "Today is a lovely weather", "Nice crowd",
			"Baba Ramdev hmm, interesting...", "Someone is political today",
			"Its rains heavy in Pune", "Saurabh is funny person",
			"Man Rohit talks fast", "Synerzip I wonder what the full form is?",
			"Wonder whether I can give a talk here", "Everyone is welcome",
			"Hello all, I am Ketan", "Hi I am Rahul, I am a Geek",
			"Its chilly, I need a coffee", "Mee too ", "Need a vadapav",
			"Me to if it is hot", "Chai Garam Chai" };
	private Context context = null;

	/**
 * 
 */
	public TweetManager(final Context context) {
		this.context = context;
	}

	/**
	 * Get Tweetter feed
	 * 
	 * @param tokens
	 * @return
	 */
	public List<Tweet> getSocialFeed(String url, int id, int page, int per_page) {
		// Lets ignore userName for sake of this demo

		tweets = new ArrayList<Tweet>();
		// Lets simulate we are fetching things from server and thats why 3
		// second delay
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tweets = loadTweet(url, id, page, per_page);

		return tweets;
	}

	public List<Tweet> getTweetReply(String url, int tweetid) {
		tweets = new ArrayList<Tweet>();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		tweets = loadReplyTweet(url, tweetid);
		return tweets;
	}

	public List<Tweet> getNewTweet(String url) {
		tweets = new ArrayList<Tweet>();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		tweets = loadNewTweet(url);
		return tweets;
	}

	public List<Tweet> loadNewTweet(String url) {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(url, postParameters);
		System.out.println(result);

		return makeTweetFromResult(result);
	}

	public List<Tweet> loadReplyTweet(String url, int tweetid) {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("tweetid", Integer
				.toString(tweetid)));

		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(url, postParameters);
		System.out.println(result);

		return makeReplyFromResult(result);
	}

	public List<Tweet> loadTweet(String url, int id, int page, int per_page) {

		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("page", Integer
				.toString(page)));
		postParameters.add(new BasicNameValuePair("per_page", Integer
				.toString(per_page)));
		if (id > 0)
			postParameters.add(new BasicNameValuePair("userid", Integer
					.toString(id)));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(url, postParameters);
		System.out.println(result);
		System.out.println("page" + page);
		return makeTweetFromResult(result);

	}

	public List<Tweet> getTweets() {
		return tweets;
	}

	public List<Tweet> makeTweetFromResult(String result) {
		List<Tweet> tweets = new ArrayList<Tweet>();
		result = result.trim();
		System.out.println("result" + result.length());

		if (result.length() < 1)
			return tweets;
		try {
			JSONObject obj = new JSONObject(result);
			int length = obj.length();
			for (int i = 0; i < length; i++) {
				JSONObject object = obj.getJSONObject(Integer.toString(i));
				int tweetid = object.getInt("id");
				String urlImagine = object.getString("avatar");
				String text = object.getString("tweetText");
				String userName = object.getString("username");
				String fullName = object.getString("screenName");
				int isRetweet = object.getInt("isRetweet");
				Boolean isMyfavorite = object.getBoolean("isMyFavorite");
				Boolean isMyRetweet = object.getBoolean("isMyReTweet");
				int reply = object.getInt("replyTweet");
				System.out.println("reply" + reply);
				String url_image = object.getString("url_images");
				String time = object.getString("createdTime");
//				String time = "";
				System.out.println("reply" + url_image);
				String nameretweet;
				if (isRetweet > 0)
					nameretweet = object.getString("nameReTweet");
				else
					nameretweet = "";
				Tweet new_tweet = new Tweet(tweetid, urlImagine, text,
						userName, fullName, isRetweet);
				new_tweet.setNameRweet(nameretweet);
				new_tweet.setIsMyFavorite(isMyfavorite);
				new_tweet.setIsMyRetweet(isMyRetweet);
				new_tweet.setIsReply(reply);
				new_tweet.setPost_url_images(url_image);
				new_tweet.setTime(time);
				tweets.add(new_tweet);
			}

			// Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
			// }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tweets;
	}
	public List<Tweet> makeReplyFromResult(String result) {
		List<Tweet> tweets = new ArrayList<Tweet>();
		result = result.trim();
		System.out.println("result" + result.length());

		if (result.length() < 1)
			return tweets;
		try {
			JSONObject obj = new JSONObject(result);
			int length = obj.length();
			for (int i = 0; i < length; i++) {
				JSONObject object = obj.getJSONObject(Integer.toString(i));
				int tweetid = object.getInt("id");
				String urlImagine = object.getString("avatar");
				String text = object.getString("tweetText");
				String userName = object.getString("username");
				String fullName = object.getString("screenName");
				int isRetweet = 0;
				Boolean isMyfavorite = object.getBoolean("isMyFavorite");
				Boolean isMyRetweet = object.getBoolean("isMyReTweet");
				int reply = object.getInt("replyTweet");
				System.out.println("reply" + reply);
				String url_image = object.getString("url_images");
				String time = object.getString("createdTime");
				System.out.println("reply" + url_image);
				String nameretweet;
				if (isRetweet > 0)
					nameretweet = object.getString("nameReTweet");
				else
					nameretweet = "";
				Tweet new_tweet = new Tweet(tweetid, urlImagine, text,
						userName, fullName, isRetweet);
				new_tweet.setNameRweet(nameretweet);
				new_tweet.setIsMyFavorite(isMyfavorite);
				new_tweet.setIsMyRetweet(isMyRetweet);
				new_tweet.setIsReply(reply);
				new_tweet.setPost_url_images(url_image);
				new_tweet.setTime(time);
				tweets.add(new_tweet);
			}

			// Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
			// }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tweets;
	}


}
